package com.bdcom.autotest.interaction.step.remodel;

import com.bdcom.autotest.interaction.bean.DeviceUpdBean;
import com.bdcom.autotest.interaction.bean.TestLog;
import com.bdcom.autotest.interaction.db.DBUtil;
import com.bdcom.autotest.interaction.http.OperDevice;
import com.bdcom.autotest.interaction.http.callback.UpdateCallBack;
import com.bdcom.autotest.interaction.socket.DataUtil;
import com.bdcom.autotest.interaction.step.IStepHandler;
import com.bdcom.autotest.interaction.step.StepThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class UpgradeDev implements IStepHandler{
	private static Logger log = LoggerFactory.getLogger(UpgradeDev.class);
	@Override
	public boolean runStep(StepThread mainrun) {
		boolean result = false;
		String[] sns = DataUtil.deleteArrayNull(mainrun.getMf().getLeftp().getsns());
//		mainrun.setFail(new ArrayList<>());
		try {
			mainrun.setFail(Arrays.asList(sns));
			if (!mainrun.isEndFlag()) {
				mainrun.addLog("等待设备升级......");
				int repeatCount = mainrun.getCurrentStep().getRunTimes();
				if (repeatCount==0) {//非重试
					List<DeviceUpdBean> deviceUpdBeanList = mainrun.getDeviceUpdBeanList();
					UpdateCallBack uUpdateCallBack = new UpdateCallBack(mainrun);
					OperDevice.update(deviceUpdBeanList,uUpdateCallBack);
					try {
						while (mainrun.getUpgradeResult().size() < sns.length) {
							Thread.sleep(200);
						}
					} catch (Exception e) {
						log.error("",e);
					}
					result = isAllSuccess(mainrun);
				}else if(repeatCount<mainrun.getMaxRepeatCount()){//重试
					List<DeviceUpdBean> deviceUpdBeanList = mainrun.getUpgradeResult();
					mainrun.setUpgradeResult(new ArrayList<>());
					int failCount = 0;
					List<DeviceUpdBean> failList = new ArrayList<DeviceUpdBean>();
					for (DeviceUpdBean bean : deviceUpdBeanList) {
						if (!bean.getUdpFlag()) {//升级失败的重试
							failCount++;
							failList.add(bean);
						}
					}
					UpdateCallBack uUpdateCallBack = new UpdateCallBack(mainrun);
					if(failCount == 1){//单台重试
						OperDevice.update(failList.get(0),uUpdateCallBack);
					}else{
						OperDevice.update(failList,uUpdateCallBack);
					}
					try {
						while (mainrun.getUpgradeResult().size() < failCount) {
							Thread.sleep(200);
						}
						result = isAllSuccess(mainrun);
					} catch (Exception e) {
					}
				}else{//超过重试次数往下走
					result = true;
				}
			}
		}catch (Exception e){
			log.error("",e);
			return false;
		}finally {
			try{
				saveLog(mainrun);
			}catch (Exception e){
				mainrun.addLog("数据库插入失败");
				return false;
			}
		}
		return result;
//		return true;
	}

	private boolean isAllSuccess(StepThread mainrun){
		List<DeviceUpdBean> deviceUpdBeanList = mainrun.getUpgradeResult();
		List<String> passCardSn = new ArrayList<>();
		boolean result = true;
		for (DeviceUpdBean bean : deviceUpdBeanList) {
			if (!bean.getUdpFlag()) {
				result = false;
			}else{
				passCardSn.add(bean.getMac());
			}
		}
		mainrun.setFail(passCardSn);
		return result;
	}

	private void saveLog(StepThread mainrun){
		List<DeviceUpdBean> deviceUpdResultList = mainrun.getUpgradeResult();
		Map<String, DeviceUpdBean> macToUpdBean = mainrun.getMacToUpdBean();
		String[] sns = DataUtil.deleteArrayNull(mainrun.getMf().getLeftp().getsns());
		List<String> passSn = new ArrayList();
		List<String> failSn = new ArrayList();

		for(DeviceUpdBean updBean:deviceUpdResultList){
			if(!updBean.getUdpFlag()){
				failSn.add(updBean.getMac());
				addTestLog(updBean,"失败");
				mainrun.addLog("记录"+updBean.getMac()+"升级失败");
			}
		}
		for(String sn:sns){
			if(!failSn.contains(sn)){
				passSn.add(sn);
				addTestLog(macToUpdBean.get(sn),"成功");
				mainrun.addLog("记录"+sn+"升级成功");
			}
		}
		mainrun.setResult(passSn);
	}

	private void addTestLog(DeviceUpdBean updBean,String result){
		try{
			TestLog testLog = new TestLog();
			testLog.setWorkOrder(updBean.getWorkOrderNum());
			testLog.setOemSn(updBean.getOemsn());
			testLog.setBdSn(updBean.getBdsn());
			testLog.setMac(updBean.getMac());
			testLog.setRes(result);
			if(updBean.getResultStr()!=null){
				testLog.setDescr(updBean.getResultStr());
			}else{
				testLog.setDescr("");
			}
			testLog.setTimeStamp(System.currentTimeMillis());
			DBUtil.addTestLog(testLog);
		}catch (Exception e){
			log.error("",e);
		}

	}
}
